package com.csu.panel;

import com.csu.consts.UiConsts;
import com.csu.db.DBUtils;
import com.csu.ds.ExpdItem;
import com.csu.ds.TransactionItem;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;

public class TransactionPanel_Right extends JPanel {
    public final static int NUM = 5;
    public JLabel label_title;

    public TransactionItem[] transItems = new TransactionItem[NUM];

    public ImageIcon icon_shop;
    public ImageIcon icon_food;
    public ImageIcon icon_living;
    public ImageIcon icon_traffic;
    public ImageIcon icon_medicine;
    public ImageIcon icon_entertainment;
    public ImageIcon icon_else;

    public TransactionPanel_Right(){
        setLayout(null);
        setBackground(Color.WHITE);

        //设置标题
        label_title = new JLabel("新增支出");
        label_title.setFont(UiConsts.FONT_SUB_TITLE);
        label_title.setBounds(35, 45, 200, 30 );
        label_title.setForeground(Color.BLACK);
        label_title.setVerticalTextPosition(JLabel.TOP);
        label_title.setHorizontalTextPosition(JLabel.LEFT);

        //导入icon文件 使用相对路径
        icon_shop = new ImageIcon("photo/trans_shop.png");
        icon_shop.setImage(icon_shop.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_food = new ImageIcon("photo/trans_food.png");
        icon_food.setImage(icon_food.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_living = new ImageIcon("photo/trans_living.png");
        icon_living.setImage(icon_living.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_traffic = new ImageIcon("photo/trans_traffic.png");
        icon_traffic.setImage(icon_traffic.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_medicine = new ImageIcon("photo/trans_medicine.png");
        icon_medicine.setImage(icon_medicine.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_entertainment = new ImageIcon("photo/trans_entertainment.png");
        icon_entertainment.setImage(icon_entertainment.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        icon_else = new ImageIcon("photo/trans_payment.png");
        icon_else.setImage(icon_else.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT));

        for( int i = 0; i < NUM; i ++ ){
            transItems[i] = new TransactionItem();

//            if( i % 2 == 0 ){
//                transItems[i].label_trans_icon.setIcon(icon_else);
//            }else{
//                transItems[i].label_trans_icon.setIcon(icon_shop);
//            }

            transItems[i].label_trans_icon.setBounds(35, 95 + ( i * 90 ), 50, 50 );
            transItems[i].label_trans_person.setBounds(100, 95 + ( i * 90 ), 100, 15);
            transItems[i].label_trans_type.setBounds( 100, 115 + ( i * 90 ), 100, 15 );
            transItems[i].label_trans_date.setBounds( 100, 135 + ( i * 90 ), 100, 15 );
            transItems[i].label_trans_amount.setBounds(180, 100 + ( i * 90 ), 100, 20 );
            transItems[i].label_trans_amount.setHorizontalTextPosition(SwingConstants.RIGHT);

            add(transItems[i].label_trans_icon);
            add(transItems[i].label_trans_person);
            add(transItems[i].label_trans_type);
            add(transItems[i].label_trans_amount);
            add(transItems[i].label_trans_date);
        }

        inniRcTrans(DBUtils.getExpenditureData());

        add(label_title);

    }

    public void inniRcTrans(ArrayList<ExpdItem> expdItems){
        Collections.sort(expdItems);//按序号降序排序
        for( int i = 0 ; i < expdItems.size() && i < NUM; i ++ ){
            ExpdItem curExpdItem = expdItems.get(i);
            TransactionItem curTransItem = transItems[i];
            String type = curExpdItem.getType();
            //将date转换为字符串
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//日期格式
            String date = dateFormat.format(curExpdItem.getDate());

            //设置文字label
            curTransItem.label_trans_person.setText( curExpdItem.getPerson() );
            curTransItem.label_trans_type.setText( curExpdItem.getType() );
            curTransItem.label_trans_amount.setText( "¥ " + String.format("%.2f", curExpdItem.getAmount() ) );
            curTransItem.label_trans_date.setText(date);
            //设置icon图标
            switch (type) {
                case "购物" : curTransItem.label_trans_icon.setIcon(icon_shop);break;
                case "饮食" : curTransItem.label_trans_icon.setIcon(icon_food);break;
                case "住房" : curTransItem.label_trans_icon.setIcon(icon_living);break;
                case "出行" : curTransItem.label_trans_icon.setIcon(icon_traffic);break;
                case "医疗" : curTransItem.label_trans_icon.setIcon(icon_medicine);break;
                case "娱乐" : curTransItem.label_trans_icon.setIcon(icon_entertainment);break;
                default : curTransItem.label_trans_icon.setIcon(icon_else);break;
            }


//            transItems[i].label_trans_date
        }



    }

}
